package com.agile.springboot.service.impl;

import com.agile.springboot.dao.QuestionAnsweredMapper;
import com.agile.springboot.dao.QuestionMapper;
import com.agile.springboot.entity.Question;
import com.agile.springboot.entity.QuestionAnswered;
import com.agile.springboot.service.QuestionAnsweredService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionAnsweredServiceImpl implements QuestionAnsweredService {

    @Resource
    QuestionAnsweredMapper questionAnsweredMapper;
    @Resource
    QuestionMapper questionMapper;

    /**
     * 查询当前答者拥有的所有答卷——根据当前答者id
     * @param user_id 答者id
     * @return List<QuestionnaireAnswered> 该答者拥有的所有答卷
     */
    @Override
    public Map<String,List<QuestionAnswered>> queryQuestionnaireAnsweredBelongToAnswerer(String user_id){
        //查询当前答者拥有的所有答卷中的所答题目
        QueryWrapper<QuestionAnswered> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        map.put("answerer_id",user_id);
        List<QuestionAnswered> questionAnsweredList = questionAnsweredMapper.selectList(queryWrapper.allEq(map));

        //将所答题目划分进其所对应的答卷
        Map<String,List<QuestionAnswered>> questionnaireAnsweredList = new HashMap<>();
        for(QuestionAnswered questionAnswered : questionAnsweredList){//遍历当前答者的全部所答题目
            String key = questionAnswered.getQuestionnaire_answered_id();//当前题目所对应的答卷id
            List<QuestionAnswered> list = new ArrayList<>();
            if(questionnaireAnsweredList.containsKey(key)){//如果当前问卷id已经存入map的key值,则获取当前答卷id下所对应的所答题目列表
                list = questionnaireAnsweredList.get(key);
            }
            //将当前所答题目添加进该问卷id对应的所答题目列表
            list.add(questionAnswered);
            questionnaireAnsweredList.put(key,list);
        }
        return questionnaireAnsweredList;
    }

    /**
     * 查询当前问卷所含的所有答卷——根据当前问卷id
     * @param questionnaire_id 问卷id
     * @return Map<String,List<QuestionAnswered>> 该问卷所含的所有答卷
     */
    @Override
    public Map<String,List<QuestionAnswered>> queryQuestionnaireAnsweredBelongToQuestionnaire(String questionnaire_id){
        //查询当前问卷所含的所有答卷中的所答题目
        QueryWrapper<QuestionAnswered> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        map.put("questionnaire_id",questionnaire_id);
        List<QuestionAnswered> questionAnsweredList = questionAnsweredMapper.selectList(queryWrapper.allEq(map));

        //将所答题目划分进其所对应的答卷
        Map<String,List<QuestionAnswered>> questionnaireAnsweredList = new HashMap<>();
        for(QuestionAnswered questionAnswered : questionAnsweredList){//遍历当前问卷的全部所答题目
            String key = questionAnswered.getQuestionnaire_answered_id();//当前题目所对应的答卷id
            List<QuestionAnswered> list = new ArrayList<>();
            if(questionnaireAnsweredList.containsKey(key)){//如果当前问卷id已经存入map的key值,则获取当前答卷id下所对应的所答题目列表
                list = questionnaireAnsweredList.get(key);
            }
            //将当前所答题目添加进该问卷id对应的所答题目列表
            list.add(questionAnswered);
            questionnaireAnsweredList.put(key,list);
        }
        return questionnaireAnsweredList;
    }

    /**
     * 查询答卷单卷——根据答卷id
     * @param questionnaire_answered_id 答卷id
     * @return Map<String,List<QuestionAnswered>> 该id对应的单卷
     */
    @Override
    public Map<String,List<QuestionAnswered>> queryQuestionnaireAnsweredById(String questionnaire_answered_id){
        //查询单卷的所有题目
        QueryWrapper<QuestionAnswered> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        map.put("questionnaire_answered_id",questionnaire_answered_id);
        List<QuestionAnswered> questionAnsweredList = questionAnsweredMapper.selectList(queryWrapper.allEq(map));

        //将题目划分进该问卷
        Map<String,List<QuestionAnswered>> questionnaireAnswered = new HashMap<>();
        String key = questionnaire_answered_id;
        questionnaireAnswered.put(key,questionAnsweredList);

        return questionnaireAnswered;
    }

    @Override
    public List<QuestionAnswered> queryAnswersByQuestionDomainType(String question_domain_type){
        //先根据当前问题的相关领域将所有相同相关领域的问题查询出来
        QueryWrapper<Question> queryWrapper_question = new QueryWrapper<>();
        Map<String,Object> map_question = new HashMap<>();
        map_question.put("domain_type",question_domain_type);
        List<Question> questionList = questionMapper.selectList(queryWrapper_question.allEq(map_question));
        //存储该领域问题下的答题列表
        List<QuestionAnswered> questionAnsweredList = new ArrayList<>();
        for(Question question : questionList){
            //查询当前问题下的所有答题
            QueryWrapper<QuestionAnswered> queryWrapper_questionAnswered = new QueryWrapper<>();
            Map<String,Object> map_questionAnswered = new HashMap<>();
            map_questionAnswered.put("question_id",question.getId());
            List<QuestionAnswered> newList = questionAnsweredMapper.selectList(queryWrapper_questionAnswered.allEq(map_questionAnswered));
            questionAnsweredList.addAll(newList);//将新答题列表添加进总答题列表
        }
        return questionAnsweredList;
    }

    /**
     * 查询单题的所有答卷——根据问题id
     * @param question_id 问题id
     * @return List<QuestionAnswered> 该id对应的所有答题
     */
    @Override
    public List<QuestionAnswered> queryQuestionAnsweredByQuestionId(String question_id){
        //查询该问题对应的所有答题
        QueryWrapper<QuestionAnswered> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        map.put("question_id",question_id);
        List<QuestionAnswered> questionAnsweredList = questionAnsweredMapper.selectList(queryWrapper.allEq(map));

        return  questionAnsweredList;
    }
}
